package com.magnificent.atlantis.client.data.serialization;

import com.magnificent.atlantis.client.data.alert.AbstractAlert;
import com.magnificent.atlantis.client.data.alert.AlertsContainer;
import com.magnificent.atlantis.client.data.alert.AutomaticAlert;
import com.magnificent.atlantis.client.data.alert.ManualAlert;
import com.magnificent.atlantis.client.data.configuration.ConfigurationContainer;
import com.magnificent.atlantis.client.data.configuration.FileConfiguration;
import com.magnificent.atlantis.client.data.configuration.LocalConfiguration;
import com.magnificent.atlantis.client.data.configuration.ServerConfiguration;
import com.magnificent.atlantis.client.data.message.MessageType;
import com.magnificent.atlantis.client.data.message.MessagesContainer;
import com.magnificent.atlantis.client.data.message.messages.*;
import com.magnificent.atlantis.client.data.settings.SettingsContainer;
import com.magnificent.atlantis.client.data.settings.User;
import com.magnificent.atlantis.client.data.task.creator.AbstractCreatorTask;
import com.magnificent.atlantis.client.data.task.creator.CreatorBasicTask;
import com.magnificent.atlantis.client.data.task.creator.CreatorMeetingTask;
import com.magnificent.atlantis.client.data.task.planner.BasicPlannerTask;
import com.magnificent.atlantis.client.data.task.planner.CommunityMeetingPlannerTask;
import com.magnificent.atlantis.client.data.task.planner.SimpleMeetingPlannerTask;
import com.magnificent.atlantis.client.ui.planner.PlannerData;
import com.magnificent.commons.utils.settings.JAXBUtils;

/**
 * User: Babanin
 * Date: 13.02.2009
 * Time: 15:15:07
 */
public class Serializer {
    private static final Class[] CONFIGURATION_DATA_CLASSES = new Class[]{
            ConfigurationContainer.class, LocalConfiguration.class, CommunityMeetingPlannerTask.class,
            ServerConfiguration.class, BasicPlannerTask.class, SimpleMeetingPlannerTask.class
    };

    private static final Class[] SETTINGS_DATA_CLASSES = new Class[]{
            SettingsContainer.class, User.class
    };

    private static final Class[] ALERTS_CLASSES = new Class[]{
            AlertsContainer.class, AbstractAlert.class, AutomaticAlert.class, ManualAlert.class
    };

    private static final Class[] MESSAGES_CLASSES = new Class[]{
            MessagesContainer.class, ClientMessage.class, MessageType.class,
            ClientTextMessage.class, ClientAcceptMeetingMessage.class, ClientCancelMeetingMessage.class,
            ClientUpdateMeetingMessage.class, ClientInviteMessage.class, ClientNewMeetingMessage.class,
            ClientReInviteMessage.class
    };

    private static final Class[] FILECONF_CLASSES = new Class[]{
            FileConfiguration.class, AbstractCreatorTask.class, CreatorBasicTask.class, CreatorMeetingTask.class
    };

    public static PlannerData loadPlannerData(String settingsPath, String confPath,
                                              String eventsPath, String messagesPath) {
        return new PlannerData(loadEvents(eventsPath), loadSettings(settingsPath), loadMessages(messagesPath),
                loadConfigurations(confPath));
    }

    public static void savePlannerData(PlannerData plannerData, String settingsPath, String confPath,
                                       String eventsPath, String messagesPath) {
        saveEvents(plannerData.getAlertsContainer(), eventsPath);
        saveMessages(plannerData.getMessagesContainer(), messagesPath);
        saveSettings(plannerData.getSettingsContainer(), settingsPath);
        saveConfigurations(plannerData.getConfigurationContainer(), confPath);
    }

    public static FileConfiguration loadFileConfiguration(String path) {
        try {
            Object obj = JAXBUtils.loadSettings(path, FILECONF_CLASSES);
            return obj == null ? null : (FileConfiguration) obj;
        } catch (Exception e) {
            return null;
        }
    }

    public static AlertsContainer loadEvents(String path) {
        Object obj = JAXBUtils.loadSettings(path, ALERTS_CLASSES);
        return obj == null ? new AlertsContainer() : (AlertsContainer) obj;
    }

    public static MessagesContainer loadMessages(String path) {
        Object obj = JAXBUtils.loadSettings(path, MESSAGES_CLASSES);
        return obj == null ? new MessagesContainer() : (MessagesContainer) obj;
    }

    public static SettingsContainer loadSettings(String path) {
        Object obj = JAXBUtils.loadSettings(path, SETTINGS_DATA_CLASSES);
        return obj == null ? new SettingsContainer() : (SettingsContainer) obj;
    }

    public static ConfigurationContainer loadConfigurations(String path) {
        Object obj = JAXBUtils.loadSettings(path, CONFIGURATION_DATA_CLASSES);
        return obj == null ? new ConfigurationContainer() : (ConfigurationContainer) obj;
    }

    public static void saveEvents(AlertsContainer alertsContainer, String path) {
        JAXBUtils.saveSettings(path, ALERTS_CLASSES, alertsContainer);
    }

    public static void saveSettings(SettingsContainer settingsContainer, String path) {
        JAXBUtils.saveSettings(path, SETTINGS_DATA_CLASSES, settingsContainer);
    }

    public static void saveMessages(MessagesContainer messagesContainer, String path) {
        JAXBUtils.saveSettings(path, MESSAGES_CLASSES, messagesContainer);
    }

    public static void saveConfigurations(ConfigurationContainer configurationContainer, String path) {
        JAXBUtils.saveSettings(path, CONFIGURATION_DATA_CLASSES, configurationContainer);
    }
}
